<template>
	<view>
		<view class="main">
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item label="旧密码">
					<u-input v-model="form.oldPassword"></u-input>
				</u-form-item>
				<u-form-item label="新密码">
					<u-input v-model="form.newPassword"></u-input>
				</u-form-item>
				<u-form-item label="确认密码" prop="repassword">
					<u-input v-model="form.repassword"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<button class="bu" style="margin-top: 40rpx;" @click="submit">修改密码</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					oldPassword: '',
					newPassword: '',
					repassword: ''
				},
				rules: {
					repassword: [
						{
							validator: (rule,value,callback) => {
								if(value === this.form.newPassword){
									callback()
								}else{
									callback(new Error('两次密码输入不一致'))
								}
							},
							trigger: ['blur','change']
						}
					]
				}
			};
		},
		methods:{
			submit(){
				this.$refs.uForm.validate(valid => {
					if(valid){
						this.$request({
							url: '/prod-api/api/common/user/resetPwd',
							method: 'PUT',
							data: {
								...this.form
							}
						}).then(res => {
							if(res.code === 200){
								uni.showToast({
									title:'修改成功'
								})
								setTimeout(() => {
									uni.switchTab({
										url:'/pages/user/user'
									})
								},1000)
							}else{
								uni.showToast({
									title:res.msg,
									icon:'error'
								})
							}
						})
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
view{
	padding: 20rpx;
	.main{
		padding: 20rpx;
	}
}
</style>
